﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace H_GAME
{
    public partial class frmPicGen : Form
    {
        ArrayList oldPicfiles = new ArrayList();  //源图片
        ArrayList oldchangePicfiles = new ArrayList();  //需要转换图片




        public frmPicGen()
        {
            InitializeComponent();
            txtOld.Text = @"D:\project\GamePro\H_GAME\H_GAME\bin\Debug\Pic\Hammer1";
            txtOldCHange.Text = @"D:\GAME\2dfm-汉化修正版\catma11\7";
            txtNew.Text = @"D:\project\GamePro\H_GAME\H_GAME\bin\Debug\Pic\Hammer7";

            DirectoryInfo folder = new DirectoryInfo(txtOld.Text);
            foreach (FileInfo file in folder.GetFiles("*.png"))
            {
                oldPicfiles.Add(file.FullName);
            }

            DirectoryInfo folder2 = new DirectoryInfo(txtOldCHange.Text);
            foreach (FileInfo file in folder2.GetFiles("*.png"))
            {
                oldchangePicfiles.Add(file.FullName);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dsf = folderBrowserDialog1.ShowDialog();
                txtOld.Text = folderBrowserDialog1.SelectedPath;
                DirectoryInfo folder = new DirectoryInfo(txtOld.Text);
                foreach (FileInfo file in folder.GetFiles("*.png"))
                {
                    oldPicfiles.Add(file.FullName);
                }
            }
            catch
            { }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dsf = folderBrowserDialog1.ShowDialog();
                txtOldCHange.Text = folderBrowserDialog1.SelectedPath;

                DirectoryInfo folder = new DirectoryInfo(txtOldCHange.Text);
                foreach (FileInfo file in folder.GetFiles("*.png"))
                {
                    oldchangePicfiles.Add(file.FullName);
                }
            }
            catch
            { }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //指定位置生成图片
             Bitmap tempOld;
             Bitmap tempOldChan;
            for (int i = 0; i < oldPicfiles.Count; i++)
            {
                for (int j = 0; j < oldchangePicfiles.Count; j++)
                {
                    tempOld = new Bitmap(oldPicfiles[i].ToString());
                    tempOldChan = new Bitmap(oldchangePicfiles[j].ToString());
                    if (MyGamma(tempOld, tempOldChan))
                    {
                        string fname = "";
                        fname = System.IO.Path.GetFileName(oldPicfiles[i].ToString());
                        if (!System.IO.File.Exists(txtNew.Text + "\\" + fname))
                            System.IO.File.Copy(oldchangePicfiles[j].ToString(), txtNew.Text + "\\" + fname);
                    }
                }
            }

            //操作已经完成
            MessageBox.Show("操作已经成功！");
        }


        /// <summary>
        /// 判断图片是否一样
        /// </summary>
        /// <param name="oldbmp"></param>
        /// <param name="oldchangeBitmp"></param>
        /// <returns></returns>
        public bool MyGamma(Bitmap oldbmp, Bitmap oldchangeBitmp)
        {
            if (oldbmp.Width == oldchangeBitmp.Width && oldbmp.Height == oldchangeBitmp.Height)
            {
                //大小一致的图片
                //像素进行判断
                for (int y = 0; y < oldbmp.Height; y++)
                {
                    for (int x = 0; x < oldbmp.Width; x++)
                    {
                        Color c = oldbmp.GetPixel(x, y);
                        Color c_ch = oldchangeBitmp.GetPixel(x, y);

                        if (c == Color.Black &&
                            c_ch != Color.Black)
                        {
                            //根据黑色部分判断
                            return false;
                        }
                    }
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dsf = folderBrowserDialog1.ShowDialog();
                txtNew.Text = folderBrowserDialog1.SelectedPath;
            }
            catch
            { }
        }
    }
}
